package cc.linker.steplink.crm.controller;


import cc.linker.steplink.crm.commons.model.PcUserInfo;
import cc.linker.steplink.crm.domain.ArticleRule;
import cc.linker.steplink.crm.domain.User;
import cc.linker.steplink.crm.merchanth5.commons.UserInfoUtil;
import cc.linker.steplink.crm.response.GlobalException;
import cc.linker.steplink.crm.response.ResponseCode;
import cc.linker.steplink.crm.service.IArticleRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

@RestController
@RequestMapping("/articlerule")
/**
 * 文章标签规则管理
 */
public class ArticleRuleController {

    @Autowired
    private IArticleRuleService iArticleRuleService;
    @Autowired
    private UserInfoUtil userInfoUtil;

    @GetMapping("/get")
    public Object get(HttpServletRequest request) throws Exception {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        return iArticleRuleService.getArticleRule(userinfo.getOrgId());
    }

    @PostMapping("/add")
    public void add(HttpServletRequest request,
                    @RequestBody Map<String, Object> rule) throws Exception {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        ArticleRule record = new ArticleRule();
        record.setCondition(StringUtils.isEmpty(rule.get("condition")) ? null : Integer.parseInt(rule.get("condition").toString()));
        record.setIfLike(StringUtils.isEmpty(rule.get("ifLike")) ? null : Integer.parseInt(rule.get("ifLike").toString()));
        record.setIfComment(StringUtils.isEmpty(rule.get("ifComment")) ? null : Integer.parseInt(rule.get("ifComment").toString()));
        record.setOpenMax(StringUtils.isEmpty(rule.get("openMax")) ? null : Integer.parseInt(rule.get("openMax").toString()));
        record.setOpenMin(StringUtils.isEmpty(rule.get("openMin")) ? null : Integer.parseInt(rule.get("openMin").toString()));
        record.setReadTimeMax(StringUtils.isEmpty(rule.get("readTimeMax")) ? null : Integer.parseInt(rule.get("readTimeMax").toString()));
        record.setReadTimeMin(StringUtils.isEmpty(rule.get("readTimeMin")) ? null : Integer.parseInt(rule.get("readTimeMin").toString()));
        record.setReadTimesMin(StringUtils.isEmpty(rule.get("readTimesMin")) ? null : Integer.parseInt(rule.get("readTimesMin").toString()));
        record.setReadTimesMax(StringUtils.isEmpty(rule.get("readTimesMax")) ? null : Integer.parseInt(rule.get("readTimesMax").toString()));
        record.setOrgId(userinfo.getOrgId());
        record.setLastAdminId(userinfo.getAdminId());
        iArticleRuleService.addArticleRule(record);
    }
}
